package com.welson.restfultoolx.model;

import java.util.HashSet;
import java.util.Set;

/**
 * 搜索选项配置
 * 用于配置搜索引擎的行为
 */
public class SearchOptions {
    private boolean caseSensitive = false;      // 是否区分大小写
    private boolean exactMatch = false;         // 是否精确匹配
    private Set<String> httpMethods = new HashSet<>(); // 过滤的HTTP方法
    private int maxResults = 50;                // 最大结果数量
    private boolean includeDeprecated = true;   // 是否包含已废弃的接口
    private boolean searchInPath = true;        // 是否在路径中搜索
    private boolean searchInMethodName = true;  // 是否在方法名中搜索
    private boolean searchInClassName = true;   // 是否在类名中搜索
    private String sortBy = "relevance";        // 排序方式：relevance, path, method, class
    private boolean ascending = true;           // 是否升序排列

    public SearchOptions() {
    }

    public SearchOptions(boolean caseSensitive, boolean exactMatch) {
        this.caseSensitive = caseSensitive;
        this.exactMatch = exactMatch;
    }

    // Getters and Setters
    public boolean isCaseSensitive() {
        return caseSensitive;
    }

    public void setCaseSensitive(boolean caseSensitive) {
        this.caseSensitive = caseSensitive;
    }

    public boolean isExactMatch() {
        return exactMatch;
    }

    public void setExactMatch(boolean exactMatch) {
        this.exactMatch = exactMatch;
    }

    public Set<String> getHttpMethods() {
        return httpMethods;
    }

    public void setHttpMethods(Set<String> httpMethods) {
        this.httpMethods = httpMethods != null ? httpMethods : new HashSet<>();
    }

    public int getMaxResults() {
        return maxResults;
    }

    public void setMaxResults(int maxResults) {
        this.maxResults = Math.max(1, Math.min(maxResults, 1000)); // 限制在1-1000之间
    }

    public boolean isIncludeDeprecated() {
        return includeDeprecated;
    }

    public void setIncludeDeprecated(boolean includeDeprecated) {
        this.includeDeprecated = includeDeprecated;
    }

    public boolean isSearchInPath() {
        return searchInPath;
    }

    public void setSearchInPath(boolean searchInPath) {
        this.searchInPath = searchInPath;
    }

    public boolean isSearchInMethodName() {
        return searchInMethodName;
    }

    public void setSearchInMethodName(boolean searchInMethodName) {
        this.searchInMethodName = searchInMethodName;
    }

    public boolean isSearchInClassName() {
        return searchInClassName;
    }

    public void setSearchInClassName(boolean searchInClassName) {
        this.searchInClassName = searchInClassName;
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy != null ? sortBy : "relevance";
    }

    public boolean isAscending() {
        return ascending;
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }

    /**
     * 添加HTTP方法过滤
     */
    public void addHttpMethod(String method) {
        if (method != null && !method.trim().isEmpty()) {
            httpMethods.add(method.toUpperCase());
        }
    }

    /**
     * 移除HTTP方法过滤
     */
    public void removeHttpMethod(String method) {
        if (method != null) {
            httpMethods.remove(method.toUpperCase());
        }
    }

    /**
     * 清空HTTP方法过滤
     */
    public void clearHttpMethods() {
        httpMethods.clear();
    }

    /**
     * 检查是否有HTTP方法过滤
     */
    public boolean hasHttpMethodFilter() {
        return !httpMethods.isEmpty();
    }

    /**
     * 检查指定的HTTP方法是否被过滤
     */
    public boolean isHttpMethodFiltered(String method) {
        return !hasHttpMethodFilter() || httpMethods.contains(method != null ? method.toUpperCase() : null);
    }

    /**
     * 创建默认搜索选项
     */
    public static SearchOptions createDefault() {
        return new SearchOptions();
    }

    /**
     * 创建快速搜索选项（用于实时搜索）
     */
    public static SearchOptions createQuick() {
        SearchOptions options = new SearchOptions();
        options.setMaxResults(20);
        options.setCaseSensitive(false);
        options.setExactMatch(false);
        return options;
    }

    @Override
    public String toString() {
        return "SearchOptions{" +
                "caseSensitive=" + caseSensitive +
                ", exactMatch=" + exactMatch +
                ", httpMethods=" + httpMethods +
                ", maxResults=" + maxResults +
                ", includeDeprecated=" + includeDeprecated +
                ", searchInPath=" + searchInPath +
                ", searchInMethodName=" + searchInMethodName +
                ", searchInClassName=" + searchInClassName +
                ", sortBy='" + sortBy + '\'' +
                ", ascending=" + ascending +
                '}';
    }
}